import { defineStore } from "pinia";
import { getSearchDetail } from "../utils/api";

type S = {
  searchDetailList: any[];
  offset: number;
  keywords: string;
  mvList: any[];
};

export const useSearchDetail = defineStore({
  id: "searchDetail",
  state: (): S => {
    return {
      searchDetailList: [],
      offset: 0,
      keywords: "",
      mvList: [],
    };
  },
  getters: {},
  actions: {
    async actionSearchDetail(
      val: string,
      type: number = 1,
      onOff: boolean = false,
      offset?: number
    ) {
      this.keywords = val;
      offset = offset ? offset : this.offset;
      let data = await getSearchDetail(val, type, offset);

      if (data.songs) {
        this.searchDetailList.push(...data.songs);
      } else {
        this.mvList.push(...data.mvs);
      }
      // if(onOff){
      //   this.searchDetailList.splice(0, this.searchDetailList.length);
      // }
      // this.searchDetailList.push(...data);
    },
    loadMore() {
      this.offset++;
      this.actionSearchDetail(this.keywords);
    },
    removeMvList() {
      this.mvList = [];
    },
  },
});
